<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\FeedbackClass;
use App\Services\FeedbackClassService;
use Illuminate\Http\Request;

class FeedbackCate extends Controller
{
    /**
     * FeedbackCate constructor.
     */
    public $feedbackCateServer;

    public function __construct()
    {
        $this->feedbackCateServer = new FeedbackClassService();
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        try {
            return $this->feedbackCateServer->getList();
        } catch (\Exception $e) {
            return $this->format([], '获取失败' . $e->getMessage());
        }
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        try {
            return $this->feedbackCateServer->add();
        } catch (\Exception $e) {
            $this->error([], '添加失败' . $e->getMessage());
        }
    }


    /**
     * @return array
     */
    public function show($id)
    {
        try {
            return $this->feedbackCateServer->show($id);
        } catch (\Exception $e) {
            return $this->error([], '获取失败' . $e->getMessage());
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        try {
            return $this->feedbackCateServer->edit($id);
        } catch (\Exception $e) {
            return $this->error('修改失败' . $e->getMessage());
        }
    }

    /**
     * 批量删除分类
     * @param $ids
     * @return array
     */
    public function destroy($ids)
    {
        try {
            return $this->feedbackCateServer->del($ids);
        } catch (\Exception $e) {
            return $this->error('删除失败');
        }
    }


    public function edit_feedback_cate_show(Request $request, FeedbackClass $feedbackClass)
    {
        $id = $request->post('id');
        $data = $feedbackClass->where('id', $id)->first();
        $feedbackClassStatus = $data['is_show'] == 1 ? 0 : 1;
        $data->is_show = $feedbackClassStatus;
        return $data->save() ? $this->success([], '修改成功!') : $this->error([], '修改失败!');
    }

    /**
     *
     */
    public function __destruct()
    {
        $this->feedbackCateServer;
        // TODO: Implement __destruct() method.
    }
}
